import request from "@/utils/request";
import { Question, Subject } from "./type";

export const createSubject = (subject: Subject, avatar: File) => {
    const formData = new FormData();
    formData.append("avatar", avatar);
    formData.append("name", subject.name);
    
    return request.post("/subject/create", formData, {
        headers: {
            'Content-Type': 'multipart/form-data',
        },
        timeout: 60000,
    });
}

export const getSubjectList = () => {
    return request.get("/subject/list");
}

export const getSubjectListByUserId = (userId: string) => {
    return request.get("/subject/list", {
        params: {
            "userId": userId
        }
    });
}

export const getSubjectInfo = (id: string) => {
    return request.get("/subject/info", {
        params: {
            "id": id
        }
    });
}

export const removeSubject = (id: string) => {
    return request.get("/subject/remove", {
        params: {
            "id": id
        }
    });
}

export const getQuestionListBySubjectId = (subjectId: string) => {
    return request.get("/question/list", {
        params: {
            "subjectId": subjectId
        }
    })
}

export const createQuestion = (subjectId: string, question: Question) => {
    return request.post("/question/create", {
        "subjectId": subjectId,
        "question": question
    })
}

export const deleteQuestion = (questionId: string) => {
    return request.delete("/question/delete", {
        params: {
            "questionId": questionId
        }
    })
}

export const getLatestQuestions = (subjectId: string) => {
    return request.get("/question/latest", {
        params: {
            "subjectId": subjectId
        }
    });
}

export const getQuestionInfo = (id: string) => {
    return request.get("/question/info", {
        params: {
            "id": id
        }
    })
}